package com.ins1st.plus.web.role;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ins1st.plus.api.system.role.entity.SysRole;
import com.ins1st.plus.api.system.role.service.SysRoleApi;
import com.ins1st.plus.base.BaseController;
import com.ins1st.plus.core.annotation.Req4Json;
import com.ins1st.plus.core.annotation.Req4Model;
import com.ins1st.plus.util.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program: ins1st-plus
 * @description: 角色控制层
 * @author: coderSun
 * @create: 2019-09-08 13:07
 **/
@Controller
@RequestMapping(value = "/sys/role")
public class SysRoleController extends BaseController {

    private static final String VIEW = "pages/sys/role/";

    @Reference
    SysRoleApi sysRoleApi;


    /**
     * 首页
     *
     * @return
     */
    @Req4Model(value = "/index")
    public String index() {
        return VIEW + "role_index.html";
    }


    /**
     * 添加页
     *
     * @return
     */
    @Req4Model(value = "/add")
    public String add() {
        return VIEW + "role_add.html";
    }

    /**
     * 修改页
     *
     * @return
     */
    @Req4Model(value = "/edit")
    public String add(Integer id, Model model) {
        SysRole sysRole = this.sysRoleApi.getById(id);
        model.addAttribute("sysRole", sysRole);
        return VIEW + "role_edit.html";
    }


    /**
     * 保存
     *
     * @param sysRole
     * @return
     */
    @Req4Json(value = "/save")
    public Object save(SysRole sysRole) {
        sysRoleApi.save(sysRole);
        return success();
    }

    /**
     * 更新
     *
     * @param sysRole
     * @return
     */
    @Req4Json(value = "/update")
    public Object update(SysRole sysRole) {
        sysRoleApi.updateById(sysRole);
        return success();
    }


    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Req4Json(value = "/delete")
    public Object delete(Integer id) {
        this.sysRoleApi.removeById(id);
        return success();
    }


    /**
     * 分页
     *
     * @param sysRole
     * @return
     */
    @Req4Json(value = "/page")
    public Object page(SysRole sysRole) {
        return PageFactory.createPage(sysRoleApi.page(sysRole, PageFactory.defaultPage()));
    }

    /**
     * 查询角色ztree
     *
     * @return
     */
    @Req4Json(value = "/queryRolesZtree")
    public Object queryRolesZtree(Integer userId) {
        return sysRoleApi.queryRolesByUserId(userId);
    }

    /**
     * 分配用户角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @Req4Json(value = "/addUserRole")
    public Object addUserRole(Integer userId, String roleIds) {
        sysRoleApi.addUserRole(userId, roleIds);
        return success();
    }

    /**
     * 分配角色菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Req4Json(value = "/addRoleMenus")
    public Object addRoleMenus(Integer roleId, String menuIds) {
        sysRoleApi.addRoleMenus(roleId, menuIds);
        return success();
    }
}
